package Pages;

import java.io.IOException;

import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;

import Const.IDSound;
import Core.GLives;
import Core.KEY;
import Core.Slide;

public class InMenu extends Slide {
	boolean isFisrtOpen = true;
	
	private Image imgbg = null, imgloa, imgfocus;
	private Image imgmenu[] = null;
	
	private int Index = 0;
	private int ITEM_HEIGHT = 30;
	private int numberOfItem = 3;
	private int iTopPaint = 0;
	
	public void init(){
		try {
			if(isFisrtOpen){
				isFisrtOpen = false;
				System.out.println("InGameMenu Init");
				imgbg = Image.createImage("/bgingame.png");
				imgloa = Image.createImage("/loa.png");
				imgfocus = Image.createImage("/focusmenu.png");
				imgmenu = new Image[numberOfItem];
				imgmenu[0] = Image.createImage("/m_choitiep.png");
				imgmenu[1] = Image.createImage("/m_amthanh.png");
				imgmenu[2] = Image.createImage("/m_thoat.png");
			}
			
			Index = 0;
			sound.stopAll();
			onResize();
			
		} catch (IOException e) {}
	}
	
	public void destroy(){
		imgbg = null;
		imgloa = null;
		for(int i = 0; i < numberOfItem; i++)
			if(imgmenu[i] != null) imgmenu[i] = null;
		imgmenu = null;
	}
	public void touchDown(int ID, int x, int y) {
		if(x < ((GLives.ScreenWidth - imgbg.getWidth())>>1) || x > ((GLives.ScreenWidth + imgbg.getWidth())>>1) || y < iTopPaint || y > iTopPaint + numberOfItem*ITEM_HEIGHT)
			return;
		int inew = (y-iTopPaint)/(ITEM_HEIGHT);
		if(inew == Index)
			onkeyDown(KEY.FIRE);
		else if(inew < numberOfItem)
			Index = inew;
	}
	public void onResize() {
		iTopPaint = (GLives.ScreenHeight - imgbg.getHeight())/2 + (imgbg.getHeight() - numberOfItem*ITEM_HEIGHT)/2;
	}
	public void paint(Graphics g){
		g.drawImage(imgbg, GLives.ScreenWidth>>1, GLives.ScreenHeight>>1, Graphics.HCENTER|Graphics.VCENTER);
		
		for(int i = 0; i < numberOfItem; i++)
			g.drawImage(imgmenu[i], GLives.ScreenWidth >> 1, iTopPaint + i*ITEM_HEIGHT + (ITEM_HEIGHT>>1), Graphics.VCENTER|Graphics.HCENTER);
		if(sound.isEnable()) g.drawImage(imgloa, (GLives.ScreenWidth >> 1) + 50, iTopPaint + 1*ITEM_HEIGHT + (ITEM_HEIGHT>>1) + 5, Graphics.VCENTER|Graphics.LEFT);
		g.drawImage(imgfocus, (GLives.ScreenWidth >> 1) - 90, iTopPaint + Index*ITEM_HEIGHT + (ITEM_HEIGHT>>1) - 10, Graphics.VCENTER|Graphics.LEFT);
	}
	public void onkeyDown(int keyCode) {
		switch(keyCode){
		case KEY.BACK:
			Index = 0;
			doAction();
			break;
		case KEY.DOWN:
			if(Index < numberOfItem-1)
				Index++;
			break;
		case KEY.UP:
			if(Index > 0)
				Index--;
			break;
		case KEY.FIRE:
			if(Index >= 0)
				doAction();
			break;
		}
	}
	
	private void doAction(){
		switch(Index){
		case 0: // Choi tiep
			backSlide();
			((Game)owned).isShowIGM = false;
			sound.resume();
			break;
		case 1: // Am thanh
			sound.ONOFF_Switch(false);
			if(sound.isEnable())
				sound.play(IDSound.SFX_CONFIRM);
			break;
		case 2: // Ve Menu
			((Game)owned).saveGame();
			changeState(State.MAIN_MENU);
			break;
		}
	}

}
